package com.tiantian.homework_6_mvc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tiantian.homework_6_mvc.mapper.UserMapper;
import com.tiantian.homework_6_mvc.pojo.PageBean;
import com.tiantian.homework_6_mvc.pojo.Result;
import com.tiantian.homework_6_mvc.pojo.User;
import com.tiantian.homework_6_mvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getAll(Integer uid, String username, String gender, Integer currentPage, Integer pageSize) {
        // 使用PageHelper设置分页数据
        PageHelper.startPage(currentPage, pageSize);
        // 查询到数据封装到pageBean中
        Page<User> userPage = (Page<User>) userMapper.selectAllUser(uid, username, gender);
        PageBean pageBean = new PageBean(userPage,currentPage,pageSize,userPage.getTotal());
        return Result.ok(pageBean);
    }

    @Override
    public Result deleteUser(Integer uid) {
        int i = userMapper.deleteUserByUid(uid);
        return Result.delete(i);
    }

    @Override
    public Result getUserByUid(Integer uid) {
        return Result.ok(userMapper.selectUserByUid(uid));
    }

    @Override
    public Result addUser(User user) {
        return Result.insert(userMapper.insertUser(user));
    }

    @Override
    public Result update(User user) {
        return Result.update(userMapper.updateUserByUid(user));
    }
}
